from rest_framework import serializers
from useropera.models import Order, Favorite, ServerCenter
from users.models import User, UserAddress
from goods.models import HuaweiGoods
from rest_framework.validators import UniqueValidator
from users.serializers import UserRegisterSerializer


class GoodForOrder(serializers.ModelSerializer):
    class Meta:
        model = HuaweiGoods
        fields = [
            "pk",
            "goodName",
            "goodCoverImg",
            "goodPrice",
            "goodShipFree",
            "goodNums",
        ]


class AddressForOrder(serializers.ModelSerializer):
    class Meta:
        model = UserAddress
        fields = ["pk", "title", "address", "mobile", "contactname", "is_defult"]


class OrderSerializer(serializers.ModelSerializer):
    goods = GoodForOrder()
    deliveraddress = AddressForOrder()

    class Meta:
        model = Order
        fields = [
            "id",
            "ordernum",
            "goods",
            "owner",
            "status",
            "goodsnums",
            "deliveraddress",
        ]
        read_only_fields = ["id", "ordernum", "owner", "status"]


class AddOrder(serializers.ModelSerializer):
    class Meta:
        model = Order
        fields = [
            "id",
            "ordernum",
            "goods",
            "owner",
            "status",
            "goodsnums",
            "deliveraddress",
        ]
        read_only_fields = ["id", "ordernum", "owner", "status"]


class UserForFavGoods(serializers.ModelSerializer):
    class Meta:
        model = HuaweiGoods
        fields = [
            "id",
            "goodName",
            "goodCoverImg",
            "goodPrice",
            "goodDetailNmae",
            "goodNums",
            "good_is_new",
            "good_is_hot",
            "goodShipFree",
        ]
        read_only_fields = [
            "id",
            "goodName",
            "goodCoverImg",
            "goodPrice",
            "goodDetailNmae",
            "goodNums",
            "good_is_new",
            "good_is_hot",
            "goodShipFree",
        ]


class UserFav(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ["id", "username"]
        read_only_fields = ["id", "username"]


class FavoriteSerializer_GET(serializers.ModelSerializer):
    goods = UserForFavGoods()
    owner = UserFav()

    class Meta:
        model = Favorite
        fields = ["id", "goods", "owner", "add_time"]
        read_only_fields = ["id", "add_time"]


class FavoriteSerializer_POST(serializers.ModelSerializer):
    class Meta:
        model = Favorite
        fields = ["id", "goods", "owner", "add_time"]
        read_only_fields = ["id", "owner", "add_time"]


class UserForServer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ["pk", "username", "email", "mobile", "sex", "age"]
        read_only_fields = ["pk", "username", "email", "mobile", "sex", "age"]


class ServerCenterSerializer_Get(serializers.ModelSerializer):
    owner = UserForServer()
    order = OrderSerializer()

    class Meta:
        model = ServerCenter
        fields = "__all__"
        read_only_fields = ["pk", "add_time", "servernumber", "owner"]


class ServerCenterSerializer_Post(serializers.ModelSerializer):
    class Meta:
        model = ServerCenter
        fields = "__all__"
        read_only_fields = [
            "pk",
            "add_time",
            "servernumber",
            "owner",
            "finishitime",
            "serverstatus",
        ]

